package com.jz.community.moduleshopping.groupbuying.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.commview.view.widget.XCRoundRectImageView;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.groupbuying.bean.GroupBuyingBean;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

public class GroupBuyingAdapter extends BaseQuickAdapter<GroupBuyingBean.EmbeddedBean.ContentBean, BaseViewHolder> {


    public GroupBuyingAdapter(int layoutResId, @Nullable List<GroupBuyingBean.EmbeddedBean.ContentBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, GroupBuyingBean.EmbeddedBean.ContentBean item) {
        ImageView iv_item_group_buying_label = helper.getView(R.id.iv_item_group_buying_label);
        RoundedImageView iv_item_group_buying_head = helper.getView(R.id.iv_item_group_buying_head);
        TextView tv_item_group_buying_name = helper.getView(R.id.tv_item_group_buying_name);
        TextView tv_item_group_buying_add = helper.getView(R.id.tv_item_group_buying_add);
        TextView tv_item_group_buying_distance = helper.getView(R.id.tv_item_group_buying_distance);
        TextView tv_item_group_buying_text = helper.getView(R.id.tv_item_group_buying_text);
        tv_item_group_buying_text.setText(item.getContent());
        XCRoundRectImageView iv_item_group_buying_goods1 = helper.getView(R.id.iv_item_group_buying_goods1);
        XCRoundRectImageView iv_item_group_buying_goods2 = helper.getView(R.id.iv_item_group_buying_goods2);
        XCRoundRectImageView iv_item_group_buying_goods3 = helper.getView(R.id.iv_item_group_buying_goods3);
        TextView tv_item_group_buying_goods_numb = helper.getView(R.id.tv_item_group_buying_goods_numb);
        LinearLayout ll_item_group_buying_goods = helper.getView(R.id.ll_item_group_buying_goods);
        TextView tv_item_group_buying_time = helper.getView(R.id.tv_item_group_buying_time);
        TextView tv_item_group_buying_count = helper.getView(R.id.tv_item_group_buying_count);
        if (item.getStatus() == 1) {
            iv_item_group_buying_label.setBackgroundResource(R.mipmap.group_buying_jielongzhong);
        } else {
            iv_item_group_buying_label.setBackgroundResource(R.mipmap.group_buying_yijieshu);
        }
        if (!TextUtils.isEmpty(item.getHeadImg())) {
            BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_head, item.getHeadImg());
        }
        if (ConverterUtils.toFloat(item.getDistance()) < 1000) {
            tv_item_group_buying_distance.setText("距" + CharacterUtils.roundByScale(Math.rint(ConverterUtils.toDouble(CharacterUtils.regularizePrice(ConverterUtils.toFloat(item.getDistance()))))) + "m");
        } else {
            String dis = CharacterUtils.regularizePrice(ConverterUtils.toFloat(item.getDistance()) / 1000);
            tv_item_group_buying_distance.setText("距" + CharacterUtils.roundByScale(Math.rint(ConverterUtils.toDouble(dis))) + "km");
        }
        tv_item_group_buying_name.setText(item.getNickName());
        tv_item_group_buying_add.setText(item.getAddress());
        if (item.getGoodsImages() != null && item.getGoodsImages().size() > 0) {
            if (item.getGoodsImages().size() == 1) {
                iv_item_group_buying_goods1.setVisibility(View.VISIBLE);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods1, item.getGoodsImages().get(0));
                iv_item_group_buying_goods2.setVisibility(View.INVISIBLE);
                iv_item_group_buying_goods3.setVisibility(View.INVISIBLE);
                tv_item_group_buying_goods_numb.setVisibility(View.GONE);
            }
            if (item.getGoodsImages().size() == 2) {
                iv_item_group_buying_goods1.setVisibility(View.VISIBLE);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods1, item.getGoodsImages().get(0));
                iv_item_group_buying_goods2.setVisibility(View.VISIBLE);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods2, item.getGoodsImages().get(1));
                iv_item_group_buying_goods3.setVisibility(View.INVISIBLE);
                tv_item_group_buying_goods_numb.setVisibility(View.GONE);
            }
            if (item.getGoodsImages().size() == 3) {
                iv_item_group_buying_goods1.setVisibility(View.VISIBLE);
                iv_item_group_buying_goods2.setVisibility(View.VISIBLE);
                iv_item_group_buying_goods3.setVisibility(View.VISIBLE);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods1, item.getGoodsImages().get(0));
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods2, item.getGoodsImages().get(1));
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods3, item.getGoodsImages().get(2));
                tv_item_group_buying_goods_numb.setVisibility(View.GONE);
            }
            if (item.getGoodsNum() > 3) {
                iv_item_group_buying_goods1.setVisibility(View.VISIBLE);
                iv_item_group_buying_goods2.setVisibility(View.VISIBLE);
                iv_item_group_buying_goods3.setVisibility(View.VISIBLE);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods1, item.getGoodsImages().get(0));
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods2, item.getGoodsImages().get(1));
                BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, iv_item_group_buying_goods3, item.getGoodsImages().get(2));
                tv_item_group_buying_goods_numb.setVisibility(View.VISIBLE);
                tv_item_group_buying_goods_numb.setText(item.getGoodsNum() + "商品");
            }
        } else {
            ll_item_group_buying_goods.setVisibility(View.GONE);
        }
        tv_item_group_buying_time.setText(item.getIntervalTime());
        tv_item_group_buying_count.setText("已售" + item.getBuyUserNum());
    }
}
